﻿using System;
using System.Security.Cryptography;
using System.Text;
public partial class manager_systemsetting_ChangePwd : System.Web.UI.Page
{
    DBHelper db = new DBHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (db.getCount("[user]", "userbh = '" + Session[AppConfig.SESSION_USER_BH].ToString() + "' and mmlogin = 1") > 0)
            {
                kq.Checked = true;
            }
            else
            {
                gb.Checked = true;
            }
        }
    }


    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string userid = Session[AppConfig.SESSION_USER_ID].ToString();
        string strUserPwd = Md5Encrypt(StringHelper.SafeSqlInput(txtPassword.Text.Trim()));
        string strtxtPasswordNew = Md5Encrypt(StringHelper.SafeSqlInput(txtPasswordNew.Text.Trim()));
        string strtxtPasswordNew2 = Md5Encrypt(StringHelper.SafeSqlInput(txtPasswordNew2.Text.Trim()));

        if (strUserPwd == "")
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("请填写原密码", Request.Url.LocalPath + Request.Url.Query));
        }
        else if (strtxtPasswordNew != strtxtPasswordNew2 || strtxtPasswordNew == "" || strtxtPasswordNew2 == "")
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("两次输入的新密码不同", Request.Url.LocalPath + Request.Url.Query));
        }

        string sql = "update [user] set userpwd='" + strtxtPasswordNew + "' where id='" + userid + "' and userpwd='" + strUserPwd + "'";
        int i = db.executeNoneQuery(sql);
        if (i > 0)
        {
            CacheHelper.clearAllCache();
            Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("原密码输入错误请重新输入", Request.Url.LocalPath + Request.Url.Query));
        }
    }
    protected void btn2_Click(object sender, EventArgs e)
    {
        string userid = Session[AppConfig.SESSION_USER_BH].ToString();
        int mm = 0;
        if (kq.Checked == true)
        {
            mm = 1;
        }
        string sql = "update [user] set mmlogin=" + mm + " where userbh='" + userid + "'";
        int i = db.executeNoneQuery(sql);
        if (i > 0)
        {
            CacheHelper.clearAllCache();
            Response.Write(CommonHelper.alertMsgAndRedirect("修改成功", Request.Url.LocalPath + Request.Url.Query));
        }
        else
        {
            Response.Write(CommonHelper.alertMsgAndRedirect("修改失败", Request.Url.LocalPath + Request.Url.Query));
        }
    }
    public string Md5Encrypt(string data)
    {
        MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
        byte[] dataHashed = md5Hasher.ComputeHash(Encoding.Default.GetBytes(data));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < dataHashed.Length; i++)
        {
            sBuilder.Append(dataHashed[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }
}